package device;

import communication.Communicator;

/**
 * A <code>Potentiometer</code> is an extension of <code>ADC</code> which
 * implements functions for detecting the rotation of a potentiometer.
 * 
 * @author Peter Kalauskas
 */
public class Potentiometer extends ADC implements Rotatable {
	/**
	 * The scale to convert from a degree to an adc value.
	 */
	private static final double VALUE_PER_DEGREE = -3.75;
	private int zeroAngle;

	public Potentiometer(byte id, Communicator comm) {
		super(id, comm);
	}

	/**
	 * Sets the current position of this <code>Potentiometer</code> to be zero
	 * degrees.
	 */
	public void zero() {
		setValue(-1);
		requestValue();
		while (getValue() == -1) {
			// wait for the adcValue to arrive
		}
		zeroAngle = getValue();
	}

	/**
	 * Gets the current angle of this <code>Potentiometer</code> in degrees.
	 * 
	 * @return the current angle as a degree.
	 */
	public double getDegree() {
		return (getValue() - zeroAngle) / VALUE_PER_DEGREE;
	}

	/**
	 * Converts a degree to an ADC value specific to this
	 * <code>Potentiometer</code>
	 * 
	 * @param degrees
	 *            the degrees to convert to an ADC value.
	 * @return the ADC value corresponding to the given degree.
	 */
	public int degreeToADCValue(double degrees) {
		return zeroAngle + (int) (degrees * VALUE_PER_DEGREE);
	}
}
